﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.XR;

#if UNITY_EDITOR
using UnityEditor;

public partial class MeshGenerator
    : MonoBehaviour
{
    [CustomEditor(typeof(MeshGenerator))]
    private class MeshGeneratorEditor
        :Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            MeshGenerator generator = target as MeshGenerator;
            if (generator == null)
                return;

            if (GUILayout.Button("Generate"))
            {
                var outputPath = EditorUtility.SaveFilePanelInProject("Generate mesh to...", "CustomMesh", "mesh", string.Empty);
                if(!string.IsNullOrEmpty(outputPath))
                {
                    generator.GenerateMesh(outputPath);
                }
            }
        }
    }

    /// <summary>
    /// 生成网格
    /// </summary>
    public void GenerateMesh(string outputPath)
    {
        Mesh mesh = AssetDatabase.LoadAssetAtPath<Mesh>(outputPath);
        if (mesh == null)
        {
            mesh = new Mesh();
            AssetDatabase.CreateAsset(mesh, outputPath);
        }

        mesh.Clear();

        //顶点
        Vector3[] vertices = new Vector3[vertexCount];
        for (int i = 0; i < vertexCount; i++)
        {
            vertices[i] = new Vector3(
                Random.Range(-0.5f, 0.5f),
                Random.Range(-0.5f, 0.5f),
                Random.Range(-0.5f, 0.5f));
        }

        //三角面
        int trianglesCount = Mathf.CeilToInt(vertexCount / 3.0f) * 3;
        int validCount = Mathf.FloorToInt(vertexCount / 3.0f) * 3;
        int[] triangles = new int[trianglesCount];
        for (int i = 0; i < trianglesCount; i++)
        {
            if (i < vertexCount)
                triangles[i] = i;
            else
            {
                int newIndex = i % vertexCount;
                triangles[i] = newIndex;
            }
        }

        mesh.vertices = vertices;
        mesh.triangles = triangles;

        if (includeUV)
        {
            Vector2[] uvs = new Vector2[vertexCount];
            for (int i = 0; i < vertexCount; i++)
            {
                var u = Random.Range(0f, 1f);
                uvs[i] = new Vector2(
                    u,
                    1f - u);
            }
            mesh.uv = uvs;
            //mesh.uv2 = uvs;
            //mesh.uv3 = uvs;
        }

        if (includeNormal)
        {
            Vector3[] normals = new Vector3[vertexCount];
            for (int i = 0; i < vertexCount; i++)
            {
                normals[i] = Random.insideUnitSphere;
            }
            mesh.normals = normals;
        }

        if (includeTangent)
        {
            Vector4[] tangents = new Vector4[vertexCount];
            for (int i = 0; i < vertexCount; i++)
            {
                tangents[i] = Random.insideUnitSphere;
                tangents[i].z = 1f;
            }
            mesh.tangents = tangents;
        }

        EditorUtility.SetDirty(mesh);
        AssetDatabase.SaveAssets();
        Debug.Log("Generate to " + outputPath, mesh);
    }
}
#endif


/// <summary>
/// 随机生成一个网格...
/// </summary>
public partial class MeshGenerator 
    : MonoBehaviour
{
    [Range(12, 10000)]
    public int vertexCount;

    public bool includeUV;
    public bool includeNormal;
    public bool includeTangent;
}
